package kr.go.nih.biomedi.pSEVA.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Login {
	private String userID;
	private String userPassword;
	private static Login instance = new Login();
	
	public String getUserID() {
		return userID;
	}
	public void setUserID(String userID) {
		this.userID = userID;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	
	private static Login getInstance() {
		if (instance == null) {
			return new Login();
		} else {
			return instance;
		}
	}
	
	private Connection getConnection() throws ClassNotFoundException, SQLException {
		String url = "jdbc:mysql://localhost/pSEVA";
		String user = "root";
		String password = "tlsehddl1";
		
		Class.forName("com.mysql.jdbc.Driver");
		Connection c = DriverManager.getConnection(url, user, password);
		
		return c;
 
	}
	
	public boolean doLogin() throws ClassNotFoundException, SQLException {
		String id = getUserID();
		String pwd = getUserPassword();

		Connection conn = getConnection();
		
		PreparedStatement ps = conn.prepareStatement("select userID, userPassword from person where userID='" + id + "' and userPassword='" + pwd + "';");
		
		ResultSet rs = ps.executeQuery();
		
		while(rs.next()) {
			String tId = rs.getString("userID");
			String tPassword = rs.getString("userPassword");
			if (tId.equals(id) && tPassword.equals(pwd)) return true;
		}
		
		rs.close();
		ps.close();
		conn.close();
		
		return false;
	}
}
